import React, {
    Component,
    PropTypes
} from 'react';

import ReactNative, {
    EdgeInsetsPropType,
    Image,
    NativeMethodsMixin,
    Platform,
    requireNativeComponent,
    StyleSheet,
    View,
    UIManager
} from 'react-native';

export default class ScanCodeView extends Component {

    static defaultProps = {
        autoStartRunning: true,
        scanSize: {
            height: 300,
            width: 300
        }
    };  // 注

    static propTypes = {
    ...View.propTypes,
        /**
         * Used to style and layout the `MapView`.  See `StyleSheet.js` and
         * `ViewStylePropTypes.js` for more info.
         */
        style: View.propTypes.style,
        contentInset: EdgeInsetsPropType,

        /**
         * 读取到Code 后回调
         */
        onCodeRead: PropTypes.func,
        /**
         * 出错时调用的接口
         */
        onError: PropTypes.func,
        /**
         * 是否进入时自动开启扫描
         */
        autoStartRunning: PropTypes.bool,
        /**
         * 设置描框的背景颜色
         */
        maskViewBackgroundColor: PropTypes.string,
        /**
         * 设置边框的颜色
         */
        maskViewBorderColorColor: PropTypes.string,

        scanSize: PropTypes.shape({
            /*
             * 扫描框的高
             */
            height: PropTypes.number.isRequired,
            /*
             * 扫描框的宽
             */
            width: PropTypes.number.isRequired
        })
    }

    constructor(props) {
        super(props);
        this.currentCodeRead = this._onCodeRead.bind(this);
        this.currentError = this._onError.bind(this);
    }

    _onCodeRead(event: Event) {
        var onCodeRead = this.props.onCodeRead;
        if (onCodeRead) {
            onCodeRead(event);
        }
    }

    _onError(event: Event) {

    }

    componentDidMount() {
    }

    //组件移除
    componentWillUnmount() {
        this.flashClose();
        this.closeScan();
    }

    /**
     * 打开扫描
     */
    openScan() {
        UIManager.dispatchViewManagerCommand(
            this.getViewHandle(),
            UIManager.ScanCodeView.Commands.start,
            []
        );
    }

    /**
     * 关闭当前照像机
     */
    closeScan() {
        UIManager.dispatchViewManagerCommand(
            this.getViewHandle(),
            UIManager.ScanCodeView.Commands.stop,
            []
        );
    }

    /**
     * 打开闪光灯
     */
    flashOpen() {
        UIManager.dispatchViewManagerCommand(
            this.getViewHandle(),
            UIManager.ScanCodeView.Commands.flashOpen,
            []
        );
    }

    /**
     * 关闭闪光灯
     */
    flashClose() {
        UIManager.dispatchViewManagerCommand(
            this.getViewHandle(),
            UIManager.ScanCodeView.Commands.flashClose,
            []
        );
    }

    getViewHandle() {
        return ReactNative.findNodeHandle(this.refs["ScanCodeNative"]);
    }

    render() {
        return <ScanCodeNative ref="ScanCodeNative" {...this.props} onCodeRead={this.currentCodeRead} onError={this.currentError} />;
    }
}

var ScanCodeNative = requireNativeComponent('ScanCodeView', ScanCodeView);